import { createSlice, PayloadAction } from '@reduxjs/toolkit';
import CommandFileNew from '@/core/command/file/new';
import CommandFileOpen from '@/core/command/file/open';
import CommandFileSave from '@/core/command/file/save';
import type { ItemType } from 'antd/es/menu/interface';
import { CommandKey } from '@/core/public';
import { merge } from 'lodash';
import CommandBase from '@/core/command/base';


export type ChildMenu = ItemType & {
  key?: CommandKey
  name?: string,
  shortcut?: string,
}

export type MenuDropdown = {
  key: string
  label: string
  items: ChildMenu[]
}

export const commandMenus: MenuDropdown[] = [
  {
    key: 'file',
    label: '文件(F)',
    items: [
      {
        type: 'item',
        key: 'file-new',
        name: '新建画布',
        shortcut: 'Ctrl+N',
      },
      {
        type: 'item',
        key: 'file-open',
        name: '打开文件',
        shortcut: 'Ctrl+O',
      },
      {
        type: 'divider',
      },
      {
        type: 'item',
        key: 'file-save',
        name: '另存为',
        shortcut: 'Ctrl+S',
      },
    ]
  },
  // {
  //   key: 'edit',
  //   label: '编辑(E)',
  //   items: [
  //     {
  //       type: 'item',
  //       key: 'edit-undo',
  //       name: '撤销',
  //       shortcut: 'Ctrl+Z'
  //     },
  //     {
  //       type: 'item',
  //       key: 'file-redo',
  //       name: '恢复',
  //       shortcut: 'Ctrl+Y'
  //     },
  //   ]
  // },
  // {
  //   key: 'layer',
  //   label: '图层(L)',
  //   items: [
  //     {
  //       type: 'item',
  //       key: 'layer-copy',
  //       name: '复制',
  //       shortcut: ''
  //     },
  //     {
  //       type: 'item',
  //       key: 'layer-delete',
  //       name: '删除',
  //       shortcut: ''
  //     },
  //     {
  //       type: 'item',
  //       key: 'layer-rename',
  //       name: '重命名',
  //       shortcut: ''
  //     },
  //   ]
  // }
]


export type CommandClassMap = {
  [key in CommandKey]: typeof CommandBase
}

export const commandClassMap: CommandClassMap = {
  'file-new': CommandFileNew,
  'file-open': CommandFileOpen,
  'file-save': CommandFileSave,
}

type InitialState = {
  list: MenuDropdown[],

  fileSave: {
    active: boolean
    form: {
      [key: string]: any
    }
  },

  fileNew: {
    active: boolean
    form: {
      [key: string]: any
    }
  },
}

const initialState: InitialState = {
  list: commandMenus,
  fileNew: {
    active: false,
    form: {}
  },
  fileSave: {
    active: false,
    form: {}
  }
}

export default createSlice({
  name: 'menus',
  initialState,
  reducers: {
    setFileNew: (state, action: PayloadAction<Partial<InitialState['fileNew']>>) => {
      merge(state.fileNew, action.payload)
    },
    setFileSave: (state, action: PayloadAction<Partial<InitialState['fileSave']>>) => {
      merge(state.fileSave, action.payload)
    },
  },
});